<template>
  <div>
    <el-table
      :data="dataList"
      style="width: 100%"
      header-cell-class-name="table-header"
      row-class-name="table-row-low"
      class="user-edit-table"
    >
      <el-table-column
        prop="createTime"
        :label="$t('user.changeTime')"
      />
      <el-table-column
        prop="type"
        :label="$t('user.changeType')"
      >
        <template #default="scope">
          <span>{{
            [
              '',
              $t('user.recharge'),
              $t('user.reward'),
              $t('user.payment'),
              $t('user.refund'),
              $t('user.platformChange'),
              $t('user.rechargeMember')
            ][scope.row.type]
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="changeBalance"
        :label="$t('user.changeBalance')"
      />
      <el-table-column
        prop="ioType"
        :label="$t('shop.ioType')"
      >
        <template #default="scope">
          <span class="tag-text">{{ [$t('shop.pay'), $t('shop.income')][scope.row.ioType] }}</span>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      v-if="dataList.length"
      :current-page="page.currentPage"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="page.pageSize"
      :total="page.total"
      layout="total, sizes, prev, pager, next, jumper"
      @update:page-size="onPageSizeChange"
      @update:current-page="onPageChange"
    />
    <!-- /分页 -->
  </div>
</template>

<script setup>
onMounted(() => {
  onGetData()
})
const defaultPage = {
  total: 0, // 总页数
  currentPage: 1, // 当前页数
  pageSize: 10 // 每页显示多少条
}
const page = ref({
  total: 0, // 总页数
  currentPage: 1, // 当前页数
  pageSize: 10 // 每页显示多少条
})
const dataList = ref([])
let userId = ''
const init = id => {
  userId = id
  Object.assign(page.value, defaultPage)
  onGetData(page.value)
}
// 获取数据
const onGetData = pageParam => {
  if (!userId) {
    return
  }
  http({
    url: http.adornUrl('/user/userBalance/getPageByUserId'),
    method: 'get',
    params: http.adornParams(
      Object.assign(
        {
          current: pageParam == null ? page.value.currentPage : pageParam.currentPage,
          size: pageParam == null ? page.value.pageSize : pageParam.pageSize
        },
        { userId }
      )
    )
  }).then(({ data }) => {
    dataList.value = data.records
    page.value.total = data.total
  })
}
// 每页数
const onPageSizeChange = val => {
  page.value.pageSize = val
  page.value.currentPage = 1
  onGetData(page.value)
}
// 当前页
const onPageChange = val => {
  page.value.currentPage = val
  onGetData(page.value)
}
defineExpose({ init })
</script>
